# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd

#将train，test中的creativeID展开,提取出点击什么app的数据

def create_creativeID():  
    print('读入数据  test')
    fi_test=pd.read_csv('data/origin/test.csv')
    print('读入数据  ad')
    fi_ad=pd.read_csv('data/origin/ad.csv')
    print('读入完成')
    
    fi_test=pd.merge(fi_test,fi_ad,how='left',on='creativeID')
    
    del fi_ad
    
    #获取所有的userID
    userID_list=fi_test['userID'].values
    userID_list=list(set(userID_list))
    
    dict_app={}
    dict_time={}
    dict_label={}

    #初始化字典
    for user in userID_list:
        dict_app[user]=''
        dict_time[user]=''    
        dict_label[user]=''
    
    list_user=fi_test['userID'].values
    list_time=fi_test['clickTime'].values
    list_app=fi_test['appID'].values
    list_label=fi_test['label'].values

    del fi_test
    
    total_num=len(list_time)
    for i in range(total_num):
        dict_app[list_user[i]]+=(str(list_app[i])+' ')
        dict_time[list_user[i]]+=(str(list_time[i])+' ')
        dict_label[list_user[i]]+=(str(list_label[i])+' ')
    
        if i%100000==0:
            print('字典已加载: %.2f %%'%(i/total_num*100))
    print('加载完成')

    
    #用于保存
    userID_list=list(dict_app.keys())
    cl_app=list(dict_app.values())
    cl_time=list(dict_time.values())
    cl_label=list(dict_label.values())
    
    df_list=pd.DataFrame({'userID':userID_list,
                          'cl_time':cl_time,
                          'cl_app':cl_app,
                          'cl_label':cl_label}) 
    
    df_list.to_csv('data/feature/test_click_detail_v1.csv',index=False)

if __name__=='__main__':
    create_creativeID()



    
